package edu.hda.pt.client.connections.netty;

import static org.jboss.netty.channel.Channels.pipeline;

import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;

import edu.hda.pt.client.PTClient;
import edu.hda.pt.client.connections.PTClientNetty;
import edu.hda.pt.utils.TestMethod;
import edu.hda.pt.utils.connections.netty.codec.MessageDecoder;
import edu.hda.pt.utils.connections.netty.codec.MessageEncoder;

/**
 * Netty pipeline factory for setting the event handlers and encoder and decoder
 * 
 * @author Marc Hammerton
 * 
 */
public class ClientPipelineFactory implements ChannelPipelineFactory {

	/**
	 * Call back Netty client
	 */
	private PTClientNetty client;

	/**
	 * Constructor setting the call back object client
	 * 
	 * @param client
	 *            - Call back object
	 */
	public ClientPipelineFactory(PTClientNetty client) {
		this.client = client;
	}

	/**
	 * Return the pipeline
	 * 
	 * @see org.jboss.netty.channel.ChannelPipelineFactory#getPipeline()
	 */
	@Override
	public ChannelPipeline getPipeline() throws Exception {
		ChannelPipeline pipeline = pipeline();

		pipeline.addLast("decoder", new MessageDecoder());
		pipeline.addLast("encoder", new MessageEncoder());

		if (PTClient.test2run == TestMethod.STREAMING) {
			pipeline.addLast("handler", new AsyncClientHandler(client));
		} else {
			pipeline.addLast("handler", new SyncClientHandler(client));
		}
		return pipeline;
	}

}
